<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="general-situation" style="width: 300px;">
        <div>
          <el-tag class="float-right p10" type="info" size="small" effect="dark" round>
           {{ browseTotal }} 浏览记录
          </el-tag>

          <el-tag class="float-right p10 mr10" type="info" size="small" effect="dark" round>
            {{ totalPage }} 汇总记录
          </el-tag>
        </div>
      </div>

      <div class="admin_main_block_select color" style="padding: 0 20px;">
        <el-form label-position="right" label-width="auto">
          <el-row :gutter="20">
            <el-col :span="4">
              <el-form-item :label="i18n('client.name')">
                <el-select
                  v-model="dataForm.client_id"
                  :placeholder="i18n('common.please_select') + i18n('client.name')" clearable @change="getDataList(model)">
                  <el-option v-for="(v, k) in clientList" :label="v.name" :key="k" :value="v.id">
                    <span class="float-left">{{ v.name }}</span>
                    <span class="float-right">{{ v.total }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="16">
              <el-form-item :label="i18n('browse.module')">
                <el-radio-group v-model="dataForm.module" @change="getDataList(model)">
                  <el-radio label="">{{ i18n('common.all') }}</el-radio>
                  <el-radio v-for="(v, k) in moduleList" :key="k" :label="v.value">
                    {{ v.text }}
                    <span class="red">({{ v.total }})</span>
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="4">
            </el-col>
            <el-col :span="16">
              <el-form-item :label="i18n('browse.name_type')">
                <el-radio-group v-model="dataForm.role_id" @change="getDataList(model)">
                  <el-radio label="">{{ i18n('common.all') }}</el-radio>
                  <el-radio v-for="(v, k) in userTypeList" :key="k" :label="v.value">
                    {{ v.text }}
                    <span class="red">({{ v.total }})</span>
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <div class="admin_table_main">
        <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle" stripe lazy>
          <el-table-column type="selection" header-align="center" align="center">
          </el-table-column>

          <el-table-column prop="id" :label="i18n('common.id')" width="70">
          </el-table-column>

          <el-table-column prop="group_text" :label="$t('browse.category.group')" width="100">
          </el-table-column>

          <el-table-column :label="i18n('browse.browse_name')" min-width="200">
            <template v-slot="scope">
              <dl class="table_dl" v-if="1 == scope.row.role_id && scope.row.user">
                <dt>
                  <el-avatar :size="45" :src="scope.row.user.avatar" />
                </dt>
                <dd class="table_dl_dd_all_30">
                  {{ scope.row.user.nickname }}
                </dd>
                <dd class="table_dl_dd_all_16_gray">
                  <span v-if="scope.row.user.organization">
                    {{ scope.row.user.organization.title }}
                  </span>
                </dd>
              </dl>
              <dl class="table_dl" v-if="2 == scope.row.role_id && scope.row.contact">
                <dt>
                  <el-avatar :size="45" :src="scope.row.contact.avatar" />
                </dt>
                <dd class="table_dl_dd_all_30">
                  {{ scope.row.contact.name }}
                </dd>
                <dd class="table_dl_dd_all_16_gray">
                  <span v-if="scope.row.contact.client">
                    {{ scope.row.contact.client.name }}
                  </span>
                </dd>
              </dl>
            </template>
          </el-table-column>

          <el-table-column :label="$t('browse.name_type')" width="100">
            <template v-slot="scope">
              {{ scope.row.role_text }}
            </template>
          </el-table-column>

          <el-table-column prop="duration_text" :label="i18n('browse.duration')" width="100">
          </el-table-column>

          <el-table-column prop="total" :label="i18n('browse.total')" width="80">
          </el-table-column>

          <el-table-column prop="module" :label="i18n('browse.module')" min-width="200">
          </el-table-column>

          <el-table-column :label="i18n('common.handle')" fixed="right" width="100">
            <template v-slot="scope">
              <el-button
                icon="Search"
                @click="router.push({ name: 'module_business_browse_list_list', query: { category_id: scope.row.id } })">
                {{ i18n('browse.info') }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="admin_table_main_pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :page-size="pageSize"
            :total="totalPage"
            :current-page="pageIndex"
            background
            layout="prev, pager, next,jumper,total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="browse-category-list">
  import { useBase } from "@/hooks/base/useBase"
  import { useTable } from "@/hooks/table/useTable"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/browse/category')

  const browseTotal = ref(0)
  const clientList = ref([])
  const moduleList = ref([])
  const userTypeList = ref([])

  const dataForm = reactive({
    client_id: '',
    role_id: '',
    module: '',
    create_time: '',
  })

  const {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    dataListLoading,
    sizeChangeHandle,
    currentChangeHandle,
    selectionChangeHandle,
    getDataList,
    handleStatus,
    deleteHandle,
  } = useTable(model.value, dataForm)

  const loadBrowseTotal = () => {
    $http({
      url: $http.adornUrl('/business/browse/total'),
      method: 'get',
      params: $http.adornParams({}),
    }).then(({data}) => {
      if (data && data.status === 200) {
        browseTotal.value = data.data[0]
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadSearchList = () => {
    $http({
      url: $http.adornUrl('/business/browse/search/data'),
      method: 'get',
      params: $http.adornParams({}),
    }).then(({data}) => {
      if (data && data.status === 200) {
        clientList.value = data.data.client
        moduleList.value = data.data.module
        userTypeList.value = data.data.user_type
      } else {
        $message.error(data.message)
      }
    })
  }

  onMounted(() => {
    dataForm.client_id = query.id

    getDataList(model)

    loadBrowseTotal()

    loadSearchList()
  })
</script>
